import {baseUrl, prefixUrl} from "../../config/api"
import Message from 'tdesign-miniprogram/message/index';

Component({
  properties: {
    templateInfo: {
      type: Object,
      value: {}
    },
  },
  data: {
    prefix:prefixUrl,
    activeValues: [],
    textareaStyle: 'height: 280rpx',
    showDialog: false,
    confirmBtn: {
      content: '知道了',
      variant: 'base'
    },
    viewer: {
      visible: false,
      showIndex: false,
      closeBtn: false,
      images: [],
    },
    showDialog: false,
  },
  methods: {
    preview(e) {
      console.log(e)
      this.setData({
        viewer: {
          images: [
            e.currentTarget.dataset.item
          ],
          showIndex: true,
          visible: true,
          closeBtn: true
        }
      });
    },
    viewerOnClose() {
      this.setData({
        viewer: {
          visible: false,
        },
      })
    },

    //下载模板图片
    DownLoad(event) {
      let _this = this;
      var path =prefixUrl + event.currentTarget.dataset.path;
      let sp = path.split('.')
      if (sp === undefined) return
      let suffix = sp[sp.length - 1]
      console.log("[开始下载----path]", path);

      if (path === undefined || path === '' || suffix === undefined || suffix === '') return
      wx.showLoading({
        title: '下载中...',
      })
      console.log('[event]', event)
      // Message.info({
      //   context: _this,
      //   offset: [90, 32],
      //   duration: 5000,
      //   content: `${suffix}`,
      // });
      wx.downloadFile({
        url: path, 
        header: {
          'content-type': `application/${suffix}`,
        },
        filePath: `${wx.env.USER_DATA_PATH}/工单模板-${Date.now()}.${suffix}`,
        success(res) {
          wx.hideLoading()
          if (res.statusCode === 200) {
            wx.openDocument({
              filePath: res.filePath,
              fileType: suffix,
              showMenu: true,
              success(res) {
                console.log(res)
              },
              fail(res) {
                Message.error({
                  context: _this,
                  offset: [90, 32],
                  duration: 5000,
                  content: res.errMsg,
                });
                console.error(res)
              }
            })
          }
        },
        fail: function (res) {
          wx.hideLoading()
          console.log('下载文件失败', res)
        }
      })
    },

    closeDialog() {
      this.setData({
        showDialog: false
      })
    },
    handleChange(e) {
      this.setData({
        activeValues: e.detail.value,
      });
    },

    openDialog() {
      this.setData({
        showDialog: true
      })
    },
  }
})